﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StockRepositoryTests.cs" company="DST Nexdox">
//   Copyright (c) DST Nexdox. All rights reserved.
// </copyright>
// <summary>
//   Defines the StockRepositoryTests type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace EFRepositoryTests
{
  using System;
  using System.Collections.Generic;
  using System.Configuration;
  using System.Data.Entity;
  using System.Linq;
  using System.Transactions;
  using BuildMeA;
  using DocProcessingRepository.Repositories;
  using Entities;
  using FluentAssertions;
  using NUnit.Framework;

  [Category("Integration")]
  [TestFixture]
  public class StockRepositoryTests
  {
    private TransactionScope transactionScope;

    private StockRepository stockRepository;

    [SetUp]
    public void Setup()
    {
      this.transactionScope = new TransactionScope();

      String connectionString = ConfigurationManager.ConnectionStrings["BBOS"].ConnectionString;
      this.stockRepository = new StockRepository(connectionString);
    }

    [TearDown]
    public void TearDown()
    {
      this.transactionScope.Dispose();
    }

    [Test]
    public void GivenValidData_WhenIGetStockByType_TheStockIsReturned()
    {
      const String DescriptionFilter = "*FilterableDescription*";

      // StockTypeId hardcoded to 1, which is LaserStock, as we do not currently have a StockType repository
      const Int32 StockTypeId = 1;

      Stock stock = BuildMeA.Stock(StockTypeId, "Code", DescriptionFilter, 0, 0, 0, "0", new DateTime(2015, 1, 1));
      this.stockRepository.Create(stock);

      List<StockEntity> stockList = this.stockRepository.GetStockByType("LaserStock", DescriptionFilter);

      stockList.Count.Should().BeGreaterOrEqualTo(1);
      stockList.SingleOrDefault(s => s.Description == DescriptionFilter).Should().NotBeNull();
    }

    [Test]
    public void GivenValidData_WhenICreateAStock_TheDatabaseIsUpdated()
    {
      Int32 originalCount = this.stockRepository.Entities.Count();
      Int32 existingMaxStockId = this.stockRepository.Entities.Max(s => s.StockID);

      StockEntity createdStock = this.stockRepository.CreateStock(1, "code", "description", 2, 3, 4, 'F', "Magenta");

      createdStock.StockId.Should().BeGreaterThan(existingMaxStockId);

      Int32 newCount = this.stockRepository.Entities.AsNoTracking().Count();
      newCount.Should().Be(originalCount + 1);
    }
  }
}